package net.springmvc19_20.controller;

import net.springmvc19_20.entity.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("stu")
public class StuController {

//    @RequestMapping(value = "hh",method = RequestMethod.GET)
    @GetMapping("hh")
    public String hh() {
        System.out.println("hh 打印了");
        return "hh";
    }

    @GetMapping("data")
    public String data(@RequestParam(value = "name") String name,
                       @RequestParam(required = false,defaultValue = "333") String sn) {
        System.out.println(name+" "+sn);
        return "hh";
    }

    // 一名多值
    @GetMapping("data1")
    public String data1(@RequestParam List<String> names) {
        System.out.println(names);
        return "hh";
    }

    // 实体对象接收
    @GetMapping("data2")
    public String data2(Stu stu) {
        System.out.println(stu);
        return "hh";
    }

    //@PathVariable注解
    @GetMapping("data3/{cid}")
    public String data3(@PathVariable(value = "cid",required = false) Integer id) {
        System.out.println(id);
        return "hh";
    }

    //@RequestBody json数据转换为java对象
    @GetMapping("data4")
    public String data4(@RequestBody Stu stu) {
        System.out.println(stu);
        return "hh";
    }

    // @CookieValue
    @GetMapping("data5")
    public String data5(@CookieValue("JSESSIONID") String sessionId) {
        System.out.println(sessionId);
        return "hh";
    }

    //@RequestHeader注解
    @GetMapping("data6")
    public String data6(@RequestHeader("Content-Type")String contentType,
                        @RequestHeader("host") String host) {
        System.out.println(contentType+" "+host);
        return "hh";
    }

    // request 级别共享域
    @GetMapping("model")
    public String model(Model model) {
        model.addAttribute("model","mode数据");
        return "hh";
    }
    @GetMapping("modelMap")
    public String model(ModelMap model) {
        model.addAttribute("modelMap","ModelMap数据");
        return "hh";
    }
    @GetMapping("map")
    public String model(Map<String, Object> map) {
        map.put("map", "map数据");
        return "hh";
    }

    @GetMapping("request")
    public String model(HttpServletRequest request) {
        request.setAttribute("request", "request数据");
        return "hh";
    }

    @GetMapping("modelAndView")
    public ModelAndView model(ModelAndView modelAndView) {
        modelAndView.setViewName("hh");
        modelAndView.addObject("modelAndView","modelAndView");
        return modelAndView;
    }
    @GetMapping("session")
    public String session(HttpSession session) {
       session.setAttribute("session","sessionValue");
        return "hh";
    }

    @Autowired
    private ServletContext servletContext;
    @GetMapping("servletContext")
    public String servletContext() {
        servletContext.setAttribute("session","servletContext");
        return "hh";
    }

    // 转发和重定向

    // 重定向
    @GetMapping("redirect")
    public String data10() {
//        request.getRequestDispatcher("/xx").forward(request, response);
//        response.sendRedirect("/xx");
        return "redirect:/index.jsp";
    }
    // 转发
    @GetMapping("forward")
    public String data11() {
        return "forward:/stu/servletContext";
    }

}
